#-------------------------------------------------
# - Library path -
-include ./Make.inc

SRC_PATH = ./src
INC_PATH = ./include
OBJ_PATH = ./_obj_

LINAL_DISPATCHER?=FALSE

# compiler
CCDEFS          =
CCINCS 		= -I$(SRC_PATH) \
	  	  -I$(INC_PATH) \
	  	  -I$(FLA_INC_PATH) 

# compilation
ifeq ($(LINAL_DISPATCHER),TRUE)
CCDEFS		+= -D LINAL_DISPATCHER_ENABLE
endif

# cuBLAS addition
ifdef CUDA_PATH
CCINCS         	+= -I$(CUDA_INC_PATH)
CCDEFS		+= -D LINAL_GPU_ENABLE 
endif

CXX_WORK 	= $(CXX) $(CFLAGS) $(EXTRA_CFLAGS) \
		  $(CCINCS) $(CCDEBUG) $(CCWARN) $(CCDEFS)

# sources
SRC_FILE	= hier/chol_nopiv.cxx \
		  hier/chol_incpiv.cxx \
		  hier/gemm.cxx \
		  hier/gemm2.cxx \
		  hier/herk.cxx \
		  hier/ldl_incpiv.cxx \
		  hier/ldl_nopiv.cxx \
		  hier/lu_incpiv.cxx \
		  hier/lu_nopiv.cxx \
		  hier/lu_piv.cxx \
		  hier/pivot.cxx \
		  hier/q.cxx \
		  hier/q2.cxx \
		  hier/q_inc.cxx \
		  hier/qr.cxx \
		  hier/qr2.cxx \
		  hier/qr_inc.cxx \
		  hier/scal.cxx \
		  hier/syrk.cxx \
		  hier/trmm.cxx \
		  hier/trsm.cxx \
		  device/host/internal/blas.cxx \
		  device/host/internal/chol.cxx \
		  device/host/internal/diag.cxx \
		  device/host/internal/ldl.cxx \
		  device/host/internal/lu.cxx \
		  device/host/internal/norm.cxx \
		  device/host/internal/pivot.cxx \
		  device/host/internal/q.cxx \
		  device/host/internal/qr.cxx \
		  dispatcher/dispatcher.cxx \
		  dispatcher/gemm.cxx \
		  dispatcher/herk.cxx \
		  dispatcher/lu_nopiv.cxx \
		  dispatcher/syrk.cxx \
		  dispatcher/trmm.cxx \
		  dispatcher/trsm.cxx \
		  util/option.cxx \
		  util/stream.cxx

ifdef CUDA_PATH
SRC_FILE	+= device/gpu/internal/blas.cxx 
endif

#
##INC		= $(addprefix $(INC_PATH)/,$(INC_FILE))
SRC 		= $(addprefix $(SRC_PATH)/,$(SRC_FILE))
OBJ 		= $(addprefix $(OBJ_PATH)/,$(SRC_FILE:.cxx=.o))

# make
.PHONY : lib

lib : liblinal.a
	@echo "LINAL Library is created at " $(LINAL_LIB_PATH)/$<

liblinal.a : all
	@echo "Creating directory " $(LINAL_LIB_PATH) 
	@echo "Use Dispatcher : " $(LINAL_DISPATCHER)
	@mkdir -p $(LINAL_LIB_PATH)
	@ar cr $(LINAL_LIB_PATH)/$@ $(OBJ)
	@ranlib $(LINAL_LIB_PATH)/$@

all : $(OBJ_PATH)/.dummy $(OBJ)

$(OBJ_PATH)/.dummy :
	@echo "Creating directory " $(OBJ_PATH) 
	@if [ -d $(OBJ_PATH) ]; then \
		touch $@; \
	else mkdir $(OBJ_PATH); touch $@; \
	fi

$(OBJ_PATH)/%.o : $(SRC_PATH)/%.cxx
	@mkdir -p $(dir $@)
	@echo "Compiling " $(CXX) " :: $<"
	@$(CXX_WORK) -o $@ -c $<

.PHONY : clean

clean:
	@(echo "Clean tests")
	@(cd ./test; make clean)
	@(echo "Clean object files and library")
	@(rm -rf $(OBJ_PATH))
	@(rm -f $(LINAL_LIB_PATH)/*.a)
	@(echo "Clean misc")
	@(rm -rf ./docs)
	@(find . -iname "*~" -exec rm {} \;)
